package com.lovestudying.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovestudying.entity.Account;
import com.lovestudying.service.AccountService;
import com.lovestudying.service.OrderService;
import com.lovestudying.service.impl.AccountServiceImpl;
import com.lovestudying.service.impl.OrderServiceImpl;
import com.lovestudying.util.DBUtil_Pro;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrderService orderService = new OrderServiceImpl();
	AccountService accountService = new AccountServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrderServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String op = request.getParameter("op");
		if (op != null) {
			switch (op) {
			case "isBought":
				isBought(request, response);
				break;
			case "doPurchase":
				doPurchase(request, response);
				break;

			default:
				break;
			}
		}
	}

	private void doPurchase(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO 自动生成的方法存根
		String courseId = request.getParameter("courseId");
		String accountId = request.getParameter("accountId");
		Integer coursePrice = Integer.parseInt(request.getParameter("coursePrice"));
		Account account = accountService.getAccountById(accountId);
		if (account.getAccount_Integral() < coursePrice) {
			response.getWriter().print(0);
		} else if (orderService.isBought(accountId, courseId)) {
			response.getWriter().print("您已购买该课程");
		} else {
			String addOrderSingleCourse = orderService.addOrderSingleCourse(DBUtil_Pro.GetUUID(), courseId, accountId,
					coursePrice, DBUtil_Pro.GetNowTime());
			if ("success".equals(addOrderSingleCourse)) {
				response.getWriter().print(1);
			} else {
				response.getWriter().print(addOrderSingleCourse);
			}
		}
	}

	private void isBought(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO 自动生成的方法存根
		String courseId = request.getParameter("courseId");
		String accountId = request.getParameter("accountId");
		if (!orderService.isBought(accountId, courseId)) {
			response.getWriter().print(false);
		} else {
			response.getWriter().print(true);
		}

	}

}
